<template>
	<view>
		<view class="footmark-list" :class="item.state=='0'?'outofdate':''">
			<view class="footmark-item" :class="item.state=='0'?'outofdate':''" v-for="(item,index) in footMarkList"
				:key="index">
				<view class="pic-container" @click="toDetail(item.productId,item.state)">
					<image :src="item.imgUrl" class="colloect-pic"></image>
				</view>
				<view class="title" @click="toDetail(item.productId,item.state)">
					{{item.productName}}
				</view>
				<view class="price">
					￥{{item.price}}
				</view>
				<view class="buy" @click="toBuy(item)" v-if="item.state=='1'">
					立即购买
				</view>
				<view class="out" v-if="item.state=='0'">
					已过期
				</view>
			</view>
		</view>
		<view class="none" v-if="!footMarkList.length">
			暂无足迹
		</view>
	</view>
</template>

<script>
	import {
		mapState,
		mapMutations
	} from 'vuex'
	export default {
		data() {
			return {
				footMarkList: [],
				offset: 1,
				totalPage: 1,
				reachFlag:false
			};
		},
		onLoad() {
			this.getFootMarks()
		},
		async onReachBottom() {
			var that = this
			if(!that.reachFlag){
				this.offset++
			}
			const {
				data: res
			} = await uni.$http.post('/user/getUserFootprintList', {
				userId: this.userInfo._id,
				"offset": this.offset,
				"limit": 10
			})
			if (res.data.list.length > 0) {
				this.footMarkList.push(...res.data.list)
			}else{
				that.reachFlag= true
				uni.$showMsg('暂无更多数据!')
			}


			// res.data.list.forEach(item => {
			// 	that.footMarkList.forEach(itm => {
			// 		if (itm.productId != item.productId) {
			// 			that.footMarkList.push(item)
			// 		}
			// 	})
			// })
			
		},
		methods: {
			// 立即购买
			async toBuy(item) {
				console.log(item)
				// 先判断是否登录
				// 1.登录情况下  加入购物车，并导航到购物车页面 
				if (JSON.stringify(this.userInfo) !== '{}') {
					const {
						data: res
					} = await uni.$http.post('/cart/addCart', {
						userId: this.userInfo._id,
						productId: item.productId,
						num: '1'
					})
					if (res.success == '0') {
						this.cartNum++
						uni.switchTab({
							url: '/pages/cart/cart'
						})
						return
					}
					// 未登录情况：导航到登录页  登陆后在导航回去
				} else {
					let redirect = '/subpkg/goods-list/goods-list';
					uni.switchTab({
						url: '/pages/my/my'
					})
					uni.$showMsg('请先登录!')
					uni.setStorageSync('redirect', redirect)
				}
			},
			// 查看详情
			toDetail(id, state) {
				// uni.navigateTo({
				// 	url: '/subpkg/goods-detail/goods-detail?id=' + id
				// })
				console.log(id, state)
				if (state == '1') {
					uni.navigateTo({
						url: '/subpkg/goods-detail/goods-detail?id=' + id
					})
				} else {
					uni.$showMsg('商品已过期!')
				}
			},
			// 获取足迹列表
			async getFootMarks() {
				const {
					data: res
				} = await uni.$http.post('/user/getUserFootprintList', {
					userId: this.userInfo._id,
					"offset": 1,
					"limit": 10
				})
				console.log(res.data)
				this.footMarkList = res.data.list
				// this.footMarkList = this.footMarkList.filter(item=>{
				// 	return item.state=='1'
				// })
			}
		},
		computed: {
			...mapState('m_user', ['userInfo'])
		}
	}
</script>

<style lang="scss">
	page {
		padding-bottom: 40rpx;
	}

	.none {
		padding-left: 24rpx;
		color: #666;
	}

	.out {
		width: 130rpx;
		height: 38rpx;
		line-height: 38rpx;
		background: #ddd;
		border-radius: 19rpx;
		font-size: 22rpx;
		font-weight: 400;
		color: #fff;
		text-align: center;
	}

	.outofdate {
		z-index: 99999 !important;
		color: #ccc !important;
	}

	.footmark-list {
		display: flex;
		justify-content: space-between;
		padding: 0 24rpx 0rpx;
		flex-wrap: wrap;

		.footmark-item {
			// height: 570rpx;
			display: flex;
			flex-direction: column;
			align-items: center;
			justify-content: center;
			margin-top: 45rpx;

			.colloect-pic {
				width: 342rpx;
				height: 342rpx;
				vertical-align: middle;
				// margin-bottom: 20rpx;
			}

			.pic-container {
				width: 342rpx;
				height: 342rpx;
			}

			.title {
				font-size: 28rpx;
				font-weight: 500;
				color: #333333;
				margin-top: 20rpx;
				width: 340rpx;
				height: 75rpx;
				text-align: center;
				overflow: hidden;
			}

			.price {
				font-size: 30rpx;
				font-weight: 400;
				color: #FF1111;
				margin-bottom: 10rpx;
			}

			.buy {
				width: 130rpx;
				height: 38rpx;
				line-height: 38rpx;
				background: #E60505;
				border-radius: 19rpx;
				font-size: 22rpx;
				font-weight: 400;
				color: #fff;
				text-align: center;

			}
		}
	}
</style>
